package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * http://www.xxcb.cn/ 潇湘晨报数字报
 *  abc
 */
//http://www.xxcb.cn/event/hunan/2014-02-10/8904874.html
public class XxcbNewsAnalyse implements AnalyseNews{
	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://xxcb.cn/show.asp[?]id=[0-9]*",
				"http://www.xxcb.cn/show.asp[?]id=[0-9]*",
				"http://www.xxcb.cn/event/[a-z]*/[0-9]{4}-[0-9]{2}-[0-9]{2}/[0-9]*[_0-9]*.html"
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);

		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		doc.select("[style*=display:none]").remove();
		doc.select(".relatedlinks").remove();
		doc.select(".sjtj").remove();
		doc.select(".at_content_side").remove();

		title =doc.select("h1").text();
		date=DateUtils.matchDate( doc.select("div.floatLeft").text());
		if (date==null){
			date=JSoupUtils.matchDate(doc, "来源:");
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div#newstext"));

		if (StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("#endPc p"));
		}
		author=JSoupUtils.matchAuthor(doc, "来源:");


		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		XxcbNewsAnalyse a=new XxcbNewsAnalyse();
		String url="http://www.xxcb.cn/event/hunan/2014-05-14/8918883.html";
		String fromatUrl=UrlArgumentTop.FromatUrl(url);
		System.out.println("url="+fromatUrl);
		System.out.println(a.isDetailPage(fromatUrl));
		UrlMeta meta=CrawlHTML.responseToURL(fromatUrl);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
